<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增活动课内容')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: summernote-css" />
    <th:block th:include="include :: bootstrap-fileinput-css" />
    <link th:href="@{/css/simplemde.min.css}" rel="stylesheet"/>
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-courseContent-add">

            <div class="form-group">
                <label class="col-sm-3 control-label">课程ID：</label>
                <div class="col-sm-8">
                    <input name="courseId" class="form-control" type="text" th:value="${courseId}" required>
                    <input name="detailsType" class="form-control" type="hidden" th:value="${detailsType}" required>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">显示顺序：</label>
                <div class="col-sm-8">
                    <input name="orderNum"  class="form-control" type="text">
                </div>
            </div>
<!--            <div class="form-group">-->
<!--                <label class="col-sm-3 control-label">时长：</label>-->
<!--                <div class="col-sm-8">-->
<!--                    <input name="durations"  class="form-control" type="text">-->
<!--                </div>-->
<!--            </div>-->

            <div class="form-group">
                <label class="col-sm-3 control-label">菜单样式：</label>
                <div class="col-sm-8">
                    <select id="menuStyle" name="menuStyle" class="form-control m-b" th:with="type=${@dict.getType('menuStyle')}">
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">子集类型：</label>
                <div class="col-sm-8">
                    <select name="parentType" class="form-control m-b"  th:with="type=${@dict.getType('parentType')}">
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                    </select>
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-3 control-label">类型：</label>
                <div class="col-sm-8">
                    <select name="categoryType" class="form-control m-b"  th:with="type=${@dict.getType('course_categoryType')}">
                        <option value="0">本课程</option>
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                    </select>
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-3 control-label">标题：</label>
                <div class="col-sm-8">
                    <input name="title" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label"></label>
                <div class="col-sm-8">
                    <input id="multipleFile" name="files" type="file" multiple>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">内容：</label>
                <div class="col-sm-8"  id="contentText">
                    <input type="hidden" class="form-control" id="content" name="content">
                    <textarea ></textarea>
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-3 control-label">注意事项：</label>
                <div class="col-sm-8">
                    <input name="remark"  class="form-control" type="text">
                </div>
            </div>
        </form>
    </div>

    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <th:block th:include="include :: summernote-js" />
    <th:block th:include="include :: bootstrap-fileinput-js" />
    <script th:src="@{/js/simplemde.min.js}"></script>
    <script th:inline="javascript">
        var simplemde = new SimpleMDE();
        var prefix = ctx + "system/courseContent"
        $("#form-courseContent-add").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                var testPlain = simplemde.value();
                $("#content").val(testPlain);
                $.operate.save(prefix + "/add", $('#form-courseContent-add').serialize());
            }
        }

        $("input[name='created']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

        $(function() {
            $('.summernote').summernote({
                lang: 'zh-CN',
                dialogsInBody: true,
                callbacks: {
                    onChange: function(contents, $edittable) {
                        $("input[name='" + this.id + "']").val(contents);
                    },
                    onImageUpload: function(files) {
                        var obj = this;
                    	var data = new FormData();
                    	data.append("file", files[0]);
                    	$.ajax({
                            type: "post",
                            url: ctx + "common/upload",
                    		data: data,
                    		cache: false,
                    		contentType: false,
                    		processData: false,
                    		dataType: 'json',
                    		success: function(result) {
                    		    if (result.code == web_status.SUCCESS) {
                    		        $('#' + obj.id).summernote('insertImage', result.url);
                    		    } else {
                    		        $.modal.alertError(result.msg);
                    		    }
                    		},
                    		error: function(error) {
                    		    $.modal.alertWarning("图片上传失败。");
                    		}
                    	});
                    }
                }
            });
            getMenuStyle();
        });
        // // 编辑框按键弹起事件
        // document.getElementById('contentText').onmouseout = function () {
        //     // 获取选定对象
        //     var selection = getSelection();
        //     var startPos = selection.selectionStart;
        //     var endPos = selection.selectionEnd;
        //     debugger;
        // }

        $("#multipleFile").fileinput({
            uploadUrl: '/system/courseContent/importImgList',
            uploadAsync: false
        }).on('filebatchuploadsuccess', function (event, data, previewId, index) {
            var rsp = data.response;
            var testPlain = simplemde.value();
            var urls  = rsp.urls.split(",");
            var url = "";
            for (let i = 0; i < urls.length; i++) {
                url = url + "![]("+urls[i]+")";
            }
            // insertAtCursor(url);
            $.modal.alertWarning(url);
            // simplemde.value(testPlain+url);
            log.info("return urls：" + rsp.urls)
        }).on('fileremoved', function (event, id, index) {
            $("input[name='" + event.currentTarget.id + "']").val('')
        })
        function insertAtCursor( myValue) {
            var  myField = document.getElementById('contentText');
        //IE support
            var testPlain = simplemde.value();
            debugger;
            this.lsvSortingHeadList.Focus();
            this.lsvSortingHeadList.Items[0].Selected = true;
            var l =  window.getSelection().anchorOffset;
            var txtoo = window.getSelection().anchorNode.previousSibling;
            var txtoo = window.getSelection().anchorNode.parentElement.previousSibling;
            var  thisTagName = window.getSelection().anchorNode.tagName;
            if (document.selection) {
                myField.focus();
                sel = document.selection.createRange();
                sel.text = myValue;
                sel.select();
            }
        //MOZILLA/NETSCAPE support
            else if (myField.selectionStart || myField.selectionStart == '0') {
                debugger;
                var startPos = myField.selectionStart;
                var endPos = myField.selectionEnd;
        // save scrollTop before insert www.keleyi.com
                var restoreTop = myField.scrollTop;
                simplemde.value(testPlain.substring(0, startPos) + myValue + testPlain.substring(endPos, myField.value.length));
                if (restoreTop > 0) {
                    myField.scrollTop = restoreTop;
                }
                myField.focus();
                myField.selectionStart = startPos + myValue.length;
                myField.selectionEnd = startPos + myValue.length;
            } else {
                debugger;
                var text = myField.selectionStart; //选中文字
                var start = myField.getSelection().anchorOffset; //开始位置
                var end = myField.getSelection().focusOffset;  //结束位置
                debugger;
                simplemde.value(testPlain.substring(0, startPos) + myValue + testPlain.substring(endPos, myField.value.length));
                myField.focus();
            }
        }

    </script>
</body>
</html>